package com.prs.crm.action.hr;

import java.io.File;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.domain.hr.Dept;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.hr.Redeploy;
import com.prs.crm.domain.hr.Role;
import com.prs.crm.domain.hr.Salary;
import com.prs.crm.service.hr.DeptService;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.hr.RedeployService;
import com.prs.crm.service.hr.RoleService;
import com.prs.crm.service.hr.SalaryService;

public class RedeployAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;
	
	private Redeploy redeploy;
	private RedeployService redeployService;
	private PaginationSupport pagination;
	private Integer[] redeployIds;

	private PersonService personService;
	private PaginationSupport personPagination;
	private Person person;

	private File upload;
	private String uploadFileName;
	
	private List<Role> roles;
	private RoleService  roleService;
	
	private List<Salary> salarys;
	private SalaryService  salaryService;
	private List <Dept> depts;
    private DeptService deptService;
    private String deptIds;
    
	public List<Dept> getDepts() {
		return depts;
	}

	public void setDepts(List<Dept> depts) {
		this.depts = depts;
	}

	public DeptService getDeptService() {
		return deptService;
	}

	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}
	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public List<Salary> getSalarys() {
		return salarys;
	}

	public void setSalarys(List<Salary> salarys) {
		this.salarys = salarys;
	}

	public SalaryService getSalaryService() {
		return salaryService;
	}

	public void setSalaryService(SalaryService salaryService) {
		this.salaryService = salaryService;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	@Action("listBookRedeploy")
	public String personExecute() {
		return SUCCESS;
	}
	@Action("listBookRedeployByDept")
	public String listBookRedeployByDept(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Person.class);
		personPagination = this.getPaginationHelper().prepare(personPagination);
		if (deptIds != null)
			criteria.add(Restrictions.in("dept", this.getPersonService().convertToDept(deptIds)));
		this.setPersonPagination(this.getPersonService().listByPage(criteria,
				personPagination));

		redeploy = new Redeploy();
		return SUCCESS;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	
	@Action("viewRedeploy")
	public String view() {
		return SUCCESS;
	}

	@Action("listRedeploy")
	public String execute() {
		return SUCCESS;
	}
	
	@Action("listRedeployByDept")
	public String listRedeployByDept(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Redeploy.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		criteria.add(Restrictions.in("dept", this.getPersonService().convertToDept(deptIds)));
		this.setPagination(this.getRedeployService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	

	@Action("editRedeploy")
	public String edit() {
		this.roles=this.getRoleService().getAllRoles();
		this.salarys=this.getSalaryService().getAllSalary();
		this.depts= this.getDeptService().getAllDept();
		return SUCCESS;
	}

	@Action(value = "saveRedeploy", results = { @Result(name = "success", type = "redirect", location = "listBookRedeploy"),
			@Result(name = "error", type = "chain", location = "editRedeploy")})
	public String save() {
		if(redeploy.getSalary()==null){
			addActionError("调动薪酬标准不能为空");
			return ERROR;
		}
		if(redeploy.getRole()==null){
			addActionError("调动职位不能为空");
			return ERROR;
		}
		if(redeploy.getDept()==null){
			addActionError("调动部门不能为空");
			return ERROR;
		}
		this.getRedeployService().saveRedeploy(redeploy, person,upload,uploadFileName);
		
		return SUCCESS;
	}

	@Action(value = "removeRedeploy", results = { @Result(name = "success", type = "redirect", location = "listRedeploy") })
	public String remove() {
		this.getRedeployService().batchRemove(redeployIds);
		return SUCCESS;
	}

	public Redeploy getRedeploy() {
		return redeploy;
	}

	public void setRedeploy(Redeploy redeploy) {
		this.redeploy = redeploy;
	}

	public RedeployService getRedeployService() {
		return redeployService;
	}

	public void setRedeployService(RedeployService redeployService) {
		this.redeployService = redeployService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getRedeployIds() {
		return redeployIds;
	}

	public void setRedeployIds(Integer[] redeployIds) {
		this.redeployIds = redeployIds;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public PersonService getPersonService() {
		return personService;
	}

	public void setPersonPagination(PaginationSupport personPagination) {
		this.personPagination = personPagination;
	}

	public PaginationSupport getPersonPagination() {
		return personPagination;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public Person getPerson() {
		return person;
	}

	public String getDeptIds() {
		return deptIds;
	}

	public void setDeptIds(String deptIds) {
		this.deptIds = deptIds;
	}

}
